package com.lv.service.utils

import com.lv.service.repo.UserInfo

/**
 * 作者： ygx
 * 创建日期：2023/12/14 4:44 PM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 * -    _              _           _     _   ____  _             _ _
 * -   / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * -  / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * - / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * -/_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 * <p>
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * 描述：
 */

/**
 * 业务员身份时的bpro
 */
fun UserInfo.getBpro(saler: String = "2"): String {
    val manager = sectionList?.manager
    val member = sectionList?.member
    if (manager != null) {
        for (i in manager.indices) {
            val isSaler = manager[i].issaler
            if (isSaler == saler) {
                return manager[i].bpro
            }
        }
    }
    if (member != null) {
        for (i in member.indices) {
            val isSaler = member[i].issaler
            if (isSaler == saler) {
                return member[i].bpro
            }
        }
    }
    return ""
}

/**
 * 是否是经理身份
 */
fun UserInfo.isManager(): Boolean {
    val size = sectionList?.manager?.size ?: 0
    return size >= 1
}

/**
 * 是否是老板
 */
fun UserInfo.isBoss(): Boolean {
    val manager = sectionList?.manager
    if (manager != null) {
        for (i in manager.indices) {
            val issaler = manager[i].issaler
            if (issaler == "0") {
                return true
            }
        }
    }
    return false
}

/**
 * 是否是自己的经理
 */
fun UserInfo.isUserManager(headerid: String?): Boolean {
    if (headerid.isNullOrEmpty()) {
        return false
    }
    val manager = sectionList?.manager
    if (manager != null) {
        for (i in manager.indices) {
            val userid = manager[i].userid
            if (userid == headerid) {
                return true
            }
        }
    }
    return false
}

/**
 * 是否是**部门的经理
 */
fun UserInfo.isUserManagerForSaler(userSaler: String?): Boolean {
    if (userSaler.isNullOrEmpty()) {
        return false
    }
    val manager = sectionList?.manager
    if (manager != null) {
        for (i in manager.indices) {
            val issaler = manager[i].issaler
            if (userSaler == issaler) {
                return true
            }
        }
    }
    return false
}

/**
 * 获取自己的所有团队（经理身份+成员身份）
 */
fun UserInfo.getUserAt(): String {
    var role = ""
    val manager = sectionList?.manager
    val member = sectionList?.member
    if (manager != null) {
        for (i in manager.indices) {
            val isSaler = manager[i].issaler
            if (!role.contains(isSaler)) {
                role += isSaler
            }
        }
    }
    if (member != null) {
        for (i in member.indices) {
            val isSaler = member[i].issaler
            if (!role.contains(isSaler)) {
                role += isSaler
            }
        }
    }

    return role
}

/**
 * 获取自己的所有团队（经理身份+成员身份）名字
 */
fun UserInfo.getUserAtName(): String {
    var role = ""
    val manager = sectionList?.manager
    val member = sectionList?.member
    if (manager != null) {
        for (i in manager.indices) {
            val isSaler = manager[i].section
            if (!role.contains(isSaler)) {
                role += "_$isSaler"
            }
        }
    }
    if (member != null) {
        for (i in member.indices) {
            val isSaler = member[i].section
            if (!role.contains(isSaler)) {
                role += "_$isSaler"
            }
        }
    }
    if (role.startsWith("_")) {
        role = role.drop(1)
    }
    //0 总经办 1 财务 2 业务  3 发行 4 设计部 5 网络部 6 运营部 7 行政
//    var memberRole = ""
//    if (role.contains("0")) {
//        memberRole += "_总经办"
//    }
//    if (role.contains("1")) {
//        memberRole += "_财务"
//    }
//    if (role.contains("2")) {
//        memberRole += "_业务"
//    }
//    if (role.contains("3")) {
//        memberRole += "_发行"
//    }
//    if (role.contains("4")) {
//        memberRole += "_设计部"
//    }
//    if (role.contains("5")) {
//        memberRole += "_网络部"
//    }
//    if (role.contains("6")) {
//        memberRole += "_运营部"
//    }
//    if (role.contains("7")) {
//        memberRole += "_行政"
//    }
//
//    if (memberRole.startsWith("_")) {
//        memberRole = memberRole.drop(1)
//    }
    return role
}

/**
 * 获取自己是否属于**团队
 */
fun UserInfo.getUserAt(isSaler: String?): Boolean {
    if (isSaler.isNullOrEmpty()) {
        return false
    }
    val manager = sectionList?.manager

    if (manager != null) {
        for (i in manager.indices) {
            val issaler = manager[i].issaler
            if (isSaler == issaler) {
                return true
            }
        }
    }
    val member = sectionList?.member
    if (member != null) {
        for (i in member.indices) {
            val issaler = member[i].issaler
            if (isSaler == issaler) {
                return true
            }
        }
    }

    return false
}

/**
 * 获取上下级关系
 * //0 总经办 1 财务 2 业务  3 发行 4 设计部 5 网络部 6 运营部 7 行政
 */
fun UserInfo.userHeader(userInfo: UserInfo?): Boolean {
    if (userInfo == null) {
        return false
    }
    if (isBoss()) {
        return true
    }
    val manager = sectionList?.manager
    val member = sectionList?.member
    var loginSaler = ""
    if (manager != null) {
        for (i in manager.indices) {
            loginSaler += manager[i].issaler
        }
    }
    if (member != null) {
        for (i in member.indices) {
            if (member[i].issaler == "0") {
                loginSaler += "0"
            }
            if (member[i].issaler == "7") {
                loginSaler += "7"
            }

        }
    }

    var userSaler = ""
    val userManager = userInfo.sectionList?.manager
    val userMember = userInfo.sectionList?.member
    if (userManager != null) {
        for (i in userManager.indices) {
            userSaler += userManager[i].issaler
        }
    }
    if (userMember != null) {
        for (i in userMember.indices) {
            if (userMember[i].issaler == "0") {
                userSaler += "0"
            }
            if (userMember[i].issaler == "7") {
                userSaler += "7"
            }
        }
    }

    if (loginSaler.contains("0")) {//login 总经办
        return !userSaler.contains("0") && !userInfo.isBoss()
    } else if (isUserManagerForSaler("7")) {//login 行政经理
        return !userInfo.isBoss() && !userSaler.contains("0") && !userInfo.isUserManagerForSaler("7")
    } else if (loginSaler.contains("7")) {//login 行政成员
        return !userInfo.isBoss() && !userSaler.contains("0") && !userInfo.isUserManagerForSaler("7") && !userSaler.contains(
            "7"
        )
    } else if (loginSaler.contains("7")) {//login 行政成员
        return !userInfo.isBoss() && !userSaler.contains("0") && !userInfo.isUserManagerForSaler("7") && !userSaler.contains(
            "7"
        )
    } else if (isManager()) {//login 其他经理
        return !userInfo.isBoss() && !userSaler.contains("0") && !userInfo.isUserManagerForSaler("7") && !userSaler.contains(
            "7"
        ) && !userInfo.isManager()
    }
    return false
}